package com.bite.bite_forum.services.Impl;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.dao.ArticleMapper;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Article;
import com.bite.bite_forum.model.Board;
import com.bite.bite_forum.services.IArticleService;
import com.bite.bite_forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    BoardServiceImpl boardService;

    @Resource
    UserServiceImpl userService;

    @Transactional
    @Override
    public void create(Article article) {
        if(article.getBoardId() == null ||article.getUserId() == null
        || StringUtils.isEmpty(article.getTitle()) || StringUtils.isEmpty(article.getContent())){
            log.info(ResultCode.ERROR_IS_NULL.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //填充数据
        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        //写入数据库
        int row = articleMapper.insertSelective(article);
        if(row != 1){
            log.info(ResultCode.FAILED_CREATE.getMessage() + " 新增失败。");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //更新
        int userRow = userService.addArticleCountById(article.getUserId());
        if(userRow != 1){
            log.info(ResultCode.FAILED_CREATE.getMessage() + " 用户帖子数更新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        int boardRow = boardService.addArticleCountById(article.getBoardId());
        if(boardRow != 1){
            log.info(ResultCode.FAILED_CREATE.getMessage() + " 板块帖子数量更新失败 。");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {

        Board board = boardService.selectById(boardId);
        if(board == null){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return articleMapper.selectAllByBoardId(boardId);
    }

    @Override
    public Article selectDetailById(Long id) {
        if(id == null || id < 0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取帖子信息
        Article article = articleMapper.selectDetailById(id);
        if(article == null){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //更新数据库帖子访问量
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setVisitCount(article.getVisitCount() + 1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //实体帖子访问量
        article.setVisitCount(article.getVisitCount() + 1);

        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {
        //校验
        if(id == null || id <= 0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //修改
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setTitle(title);
        updateArticle.setContent(content);
        updateArticle.setUpdateTime(new Date());
        //
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
    }

    @Override
    public Article selectById(Long id) {
        if(id == null || id < 0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        return article;
    }

    @Override
    public void thumbsUpById(Long id) {
        //校验参数
        if(id == null || id <=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取文章
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //修改点赞数
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setLikeCount(article.getLikeCount() + 1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        //校验
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
    }
    @Transactional
    @Override
    public void deleteById(Long id) {
        //校验参数
        if(id == null || id <=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取文章
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
//        //删除帖子
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);
        int row  = articleMapper.updateByPrimaryKeySelective(updateArticle);
         if(row != 1){
             log.info(ResultCode.ERROR_SERVICES.getMessage());
             throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
         }


    }

    @Override
    public void addArticleReplyCountById(Long id) {
        //校验参数
        if(id == null || id <=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);
        updateArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
    }

    @Override
    public  List<Article> selectByUserId(Long userId) {
        if(userId == null || userId <=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Article> result = articleMapper.selectByUserId(userId);
        return result;
    }
}
